02 / 09

List common use cases advantages and disadvantages of context api

It provides a way to pass data through the component tree without having to pass props manually at each level thus It solves the problem of prop drilling.

Advantages
  1. 1

    Built into React, so no additional libraries are required.

  2. 2

    Offers a convenient way to share state without intermediary components.

  3. 3

    Simple, easy to learn and less boilerplate code

Limitations
  1. 1

    Might not be suitable for applications with highly complex state management needs.

  2. 2

    This can lead to performance issues if not used carefully due to excessive re-renders.

  3. 3

    Limited features compared to dedicated state management libraries like Redux and MobX.

  4. 4

    It re-renders all components whenever there is any update in the provider’s value prop unlike redux which only re-renders the updated components.

Use cases
  1. 1

    Theming: If your app lets the user change its appearance (e.g. dark mode), you can put a context provider at the top of your app, and use that context in components that need to adjust their visual look.

  2. 2

    Current account and user information: Many components might need to know the currently logged-in user. Putting it in context makes it convenient to read it anywhere in the tree

  3. 3

    Managing state: As your app grows, you might end up with a lot of state closer to the top of your app. Many distant components below may want to change it. It is common to use a reducer together with context to manage complex state and pass it down to distant components without too much hassle.

  4. 4

    Context is not limited to static values. If you pass a different value on the next render, React will update all the components reading it below! This is why context is often used in combination with state.

  5. 5

    Context lets the parent component make some information available to any component in the tree below it—no matter how deep—without passing it explicitly through props.